package com.andova.empty.app;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;

import com.andova.empty.api.IEmptyDelegate;
import com.andova.empty.api.IEmptyPlugin;
import com.andova.empty.widget.EmptyLinearLayout;
import com.andova.empty.widget.EmptyRelativeLayout;
import com.andova.manager.AndovaModuleManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018-03-16.
 *
 * @author kzaxil
 * @since 1.0.0
 */
public class EmptyDelegate implements IEmptyDelegate {
    private Activity mActivity;
    private List<IEmptyPlugin> mEmptyLayoutList = new ArrayList<>(1);

    public EmptyDelegate(@NonNull Activity activity) {
        mActivity = activity;
    }

    @Override
    public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
        if (parent == null) return null;
        List<Integer> ids = AndovaModuleManager.obj().empty().validIdList(mActivity);
        if (ids == null && mEmptyLayoutList.size() > 0) return null;
        if (parent instanceof IEmptyPlugin && mEmptyLayoutList.contains(parent)) return null;
        boolean canNext = false;
        for (int i = 0; i < attrs.getAttributeCount(); i++) {
            if (ids == null || ids.size() == 0) {
                canNext = true;
                break;
            }
            if (!attrs.getAttributeName(i).equals("id")) continue;
            canNext = true;
            int id = attrs.getAttributeResourceValue(i, 0);
            if (!ids.contains(id)) return null;
        }
        if (!canNext) return null;
        name = name.lastIndexOf(".") == -1 ? name : name.substring(name.lastIndexOf("."));
        switch (name) {
            case "RelativeLayout":
                EmptyRelativeLayout erlChild = new EmptyRelativeLayout(
                        AndovaModuleManager.obj().empty().activityOption(mActivity), mActivity, attrs);
                mEmptyLayoutList.add(erlChild);
                return erlChild;
            case "LinearLayout":
                EmptyLinearLayout ellChild = new EmptyLinearLayout(
                        AndovaModuleManager.obj().empty().activityOption(mActivity), mActivity, attrs);
                mEmptyLayoutList.add(ellChild);
                return ellChild;
        }
        return null;
    }

    @Override
    public void onDestroyView() {
        mEmptyLayoutList.clear();
    }

    @Override
    public void showErrorView() {
        for (IEmptyPlugin plugin : mEmptyLayoutList) {
            plugin.showErrorView();
        }
    }

    @Override
    public void showLoadingView() {
        for (IEmptyPlugin plugin : mEmptyLayoutList) {
            plugin.showLoadingView();
        }
    }

    @Override
    public void showContentView() {
        for (IEmptyPlugin plugin : mEmptyLayoutList) {
            plugin.showContentView();
        }
    }

    @Override
    public void showEmptyView() {
        for (IEmptyPlugin plugin : mEmptyLayoutList) {
            plugin.showEmptyView();
        }
    }
}
